//
//  DeepCopy.m
//  bbmFacility
//
//  Created by Daniel Hazelbaker on 3/17/05.
//  Copyright 2005 Blue Box Moon. All rights reserved.
//

#import "DeepCopy.h"


@implementation NSObject (DeepCopy)

- (id)mutableDeepCopy
{
    if ([self respondsToSelector:@selector(mutableCopyWithZone:)] == YES)
        return [self mutableCopy];
    else if ([self respondsToSelector:@selector(copyWithZone:)] == YES)
        return [self copy];
    else
        return [self retain];
}

@end



@implementation NSDictionary (DeepCopy)

- (id)mutableDeepCopy
{
    NSMutableDictionary	*newDictionary = [[NSMutableDictionary alloc] init];
    NSEnumerator	*enumerator = [self keyEnumerator];
    id			key;


    while ((key = [enumerator nextObject]) != nil) {
	id obj = [[self objectForKey:key] mutableDeepCopy];
	[newDictionary setObject:obj forKey:key];
	[obj release];
    }

    return newDictionary;
}

@end



@implementation NSArray (DeepCopy)

- (id)mutableDeepCopy
{
    NSMutableArray  *newArray = [[NSMutableArray alloc] init];
    NSEnumerator    *enumerator = [self objectEnumerator];
    id		    obj;


    while ((obj = [enumerator nextObject]) != nil) {
	obj = [obj mutableDeepCopy];
	[newArray addObject:obj];
	[obj release];
    }

    return newArray;
}

@end



@implementation NSSet (DeepCopy)

- (id)mutableDeepCopy
{
    NSMutableSet    *newSet = [[NSMutableSet alloc] init];
    NSEnumerator    *enumerator = [self objectEnumerator];
    id		    obj;


    while ((obj = [enumerator nextObject]) != nil) {
	obj = [obj mutableDeepCopy];
	[newSet addObject:obj];
	[obj release];
    }

    return newSet;
}

@end
